package com.liangfeng.ysdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.liangfeng.ysdemo.EzvizApplication;
import com.liangfeng.ysdemo.R;
import com.liangfeng.ysdemo.adapter.MyAdapter;
import com.liangfeng.ysdemo.util.LogUtils;
import com.liangfeng.ysdemo.util.ToastUtils;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mzf on 2017/4/18.
 */

public class EZCameraListActivity_me extends AppCompatActivity {
    private ListView lv_deviceList;
    ArrayList<EZDeviceInfo> deviceList;
    private EZOpenSDK openSDK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_list);

        openSDK = EzvizApplication.getOpenSDK();

        lv_deviceList = (ListView) findViewById(R.id.lv_deviceList);

        LogUtils.e(">>>>>>>>>>><<<<<<<<<<<<<");
        LogUtils.e("AccessToken>>>>>>>>>>>" + openSDK.getEZAccessToken());

        Observable<List> observable = Observable.create(new Observable.OnSubscribe<List>() {
            @Override
            public void call(Subscriber<? super List> subscriber) {
                try {
                    //从服务器获取设备列表（网络操作）
                    deviceList = (ArrayList<EZDeviceInfo>) openSDK.getDeviceList(0, 20);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                LogUtils.e(">>>>>>>>>>><<<<<<<<<<<<<" + deviceList);
                if (deviceList != null) {

                    for (EZDeviceInfo deviceInfo : deviceList) {
                        String deviceName = deviceInfo.getDeviceName();
                        LogUtils.e("deviceName  :   " + deviceName);
                    }
                    subscriber.onNext(deviceList);
                } else {
                    LogUtils.e("未获取到设备列表");
                }
            }
        });
        Subscriber subscriber = new Subscriber<List>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(List list) {

                //设置适配器
                lv_deviceList.setAdapter(new MyAdapter(EZCameraListActivity_me.this, deviceList));
                //设置条目点击事件
                lv_deviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                        ToastUtils.showLong(EZCameraListActivity_me.this,"item"+position+"被点击了");
                        Intent intent = new Intent(EZCameraListActivity_me.this, PreviewActivity.class);
                        intent.putExtra("deviceInfo",deviceList.get(position));
                        startActivity(intent);
                    }
                });
            }
        };

        observable.subscribeOn(Schedulers.io())//订阅的操作在io线程完成
                .observeOn(AndroidSchedulers.mainThread())//订阅者的操作在主线程中完成
                .subscribe(subscriber);
    }

}
